//-----------------------------------------------------------------------
// <copyright file="OnlineDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class OnlineDAOBase
    {
        #region Common methods
        public virtual Online CreateOnlineFromReader(IDataReader reader)
        {
            Online item = new Online();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("OnlineId"))) item.OnlineId = (long)reader["OnlineId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Online"))) item.Online = (long)reader["Online"];
                if (!reader.IsDBNull(reader.GetOrdinal("Visits"))) item.Visits = (long)reader["Visits"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateOnlineFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateOnline methods
            
        public virtual void CreateOnline(Online online)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spOnlineInsert");
                
                database.AddInParameter(dbCommand, "@Online", DbType.Int64, online.Online);
                database.AddInParameter(dbCommand, "@Visits", DbType.Int64, online.Visits);
                database.AddOutParameter(dbCommand, "@OnlineId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                online.OnlineId = (long)database.GetParameterValue(dbCommand, "@OnlineId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateOnlineException, ex);
            }
        }

        #endregion

        #region UpdateOnline methods
        
        public virtual void UpdateOnline(Online online)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spOnlineUpdate");            
                
                database.AddInParameter(dbCommand, "@OnlineId", DbType.Int64, online.OnlineId);
                database.AddInParameter(dbCommand, "@Online", DbType.Int64, online.Online);
                database.AddInParameter(dbCommand, "@Visits", DbType.Int64, online.Visits);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateOnlineException, ex);
            }
        }
        
        #endregion

        #region DeleteOnline methods
        public virtual void DeleteOnline(long onlineId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spOnlineDelete");
                
                database.AddInParameter(dbCommand, "@OnlineId", DbType.Int64, onlineId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteOnlineException, ex);
            }
        }

        #endregion

        #region GetOnline methods
        
        public virtual Online GetOnline(long onlineId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spOnlineGet");
                
                database.AddInParameter(dbCommand, "@OnlineId", DbType.Int64, onlineId);
                
                Online online =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        online = CreateOnlineFromReader(reader);
                        reader.Close();
                    }
                }
                return online;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetOnlineException, ex);
            }
        }
        
        #endregion

        #region GetOnlineList methods
        public virtual OnlineCollection GetOnlineList(OnlineColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spOnlineGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                OnlineCollection onlineCollection = new OnlineCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Online online = CreateOnlineFromReader(reader);
                        onlineCollection.Add(online);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return onlineCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetOnlineListException, ex);
            }
        }
        
        public virtual OnlineCollection GetOnlineList(OnlineColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetOnlineList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetOnlineList methods
        public virtual OnlineCollection GetOnlineListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spOnlineGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                OnlineCollection onlineCollection = new OnlineCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Online online = CreateOnlineFromReader(reader);
                        onlineCollection.Add(online);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return onlineCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetOnlineListException, ex);
            }
        }
        
        public virtual OnlineCollection GetOnlineListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetOnlineListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}